package com.aaa.service.system.impl;

import com.aaa.dao.system.RoleDao;
import com.aaa.entity.system.Menu;
import com.aaa.entity.system.PageEnty;
import com.aaa.entity.system.Role;
import com.aaa.service.system.RoleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 角色信息表(Role)表服务实现类
 *
 * @author makejava
 * @since 2022-08-31 20:32:53
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;


    @Override
    public Page<Role> queryAllRolePage(PageEnty pageEnty, Role role) {
        PageHelper.startPage(pageEnty.getPage(),pageEnty.getLimit(),pageEnty.orderBy());
        List<Role> roles = roleDao.queryAllRolePage(role);
        Page<Role> page = (Page)roles;
        return page;
    }

    @Override
    public Role saveRole(Role role) {
        roleDao.saveRole(role);
        return  role;
    }

    @Override
    public Role updateRole(Role role) {
        roleDao.updateRole(role);
        return queryById(role.getRoleId());
    }

    @Override
    public int deleteRole(Integer roleId) {
        return roleDao.deleteRole(roleId);
    }

    @Override
    public boolean findRoleByName(Role role) {
        int i = roleDao.queryRoleByName(role);
        if (i == 0){
            return  true;
        }
        return false;
    }

    @Override
    public List<Menu> queryAllMenu() {
        return roleDao.queryAllMenu();
    }

    @Override
    public void insertRoleMenu(int roleId, String[] menusId) {
  for (int i=0 ; i< menusId.length;i++){
      roleDao.insertRoleMenu(roleId,menusId[i]);
  }}

    @Override
    public int deleteByRoleId(Integer roleId) {
        return 0;
    }

    @Override
    public void updateRoleMenu(Integer roleId, String[] menusId) {
         roleDao.deleteRoleMenuById(roleId);
        for (int i=0 ; i< menusId.length;i++){
            roleDao.insertRoleMenu(roleId,menusId[i]);
        }
    }

    @Override
    public List<Integer> findSelectMenu(int roleId) {
        return roleDao.findSelectMenu(roleId);
    }

    @Override
    public boolean findRoleKey(Role role) {
        int roleName = roleDao.findRoleName(role);
        if (roleName == 0){
            return  true;
        }
        return false;
    }

    @Override
    public Role queryById(Integer roleId) {
        return roleDao.queryById(roleId);
    }

    @Override
    public List<Role> queryAllRole() {
        return roleDao.queryAllRole();
    }
}
